package com.ycsj.goldmedalnewconcept;

import java.util.Timer;
import java.util.TimerTask;

import com.ycsj.goldmedalnewconcept.activity.LoginActivity;
import com.ycsj.goldmedalnewconcept.activity.QuanziCreatActivity;
import com.ycsj.goldmedalnewconcept.constant.ConfigConstant;
import com.ycsj.goldmedalnewconcept.utils.SPUtil;
import com.ycsj.goldmedalnewconcept.utils.SPUtils;
import com.ycsj.goldmedalnewconcept.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

//身份选择页
public class StartActivity extends Activity implements OnClickListener {
	private LinearLayout ll_teacher;
	private LinearLayout ll_xiaozhang;
	private LinearLayout ll_student;
	private LinearLayout ll_jiazhang;
	private LinearLayout ll_youke;
	private String role;
	private Intent intent;
	private AlertDialog alertDialog;
	private Window window;
	private int dialogWidth = 550;
	private int dialogHeight = 260;
	private WindowManager.LayoutParams lp;
	private Display mDisplay;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_start);

		getPhoneWindow();
		initView();
		setOnListener();
		initData();
	}

	private void initData() {

	}

	private void getPhoneWindow() {
		mDisplay = getWindowManager().getDefaultDisplay();

		int W = mDisplay.getWidth();
		int H = mDisplay.getHeight();

//		if (W == 720 && H == 1280) {
//			dialogWidth = 535;
//			dialogHeight = 265;
//		} else if (W == 480 && H == 800) {
//			dialogWidth = 355;
//			dialogHeight = 165;
//		} else if (W == 320 && H == 480) {
//			dialogWidth = 205;
//			dialogHeight = 115;
//		} else if (W == 1080 && H == 1920) {
//			dialogWidth = 800;
//			dialogHeight = 400;
//		} else if (W == 900 && H == 1440) {
//			dialogWidth = 700;
//			dialogHeight = 320;
//		} else if (W == 540 && H == 960) {
//			dialogWidth = 400;
//			dialogHeight = 210;
//		}
		dialogWidth = W*2/3;
		dialogHeight = dialogWidth/2;

	}

	private void setOnListener() {
		ll_teacher.setOnClickListener(this);
		ll_xiaozhang.setOnClickListener(this);
		ll_student.setOnClickListener(this);
		ll_jiazhang.setOnClickListener(this);
		ll_youke.setOnClickListener(this);
	}

	private void initView() {
		ll_teacher = (LinearLayout) findViewById(R.id.ll_teacher);
		ll_xiaozhang = (LinearLayout) findViewById(R.id.ll_xiaozhang);
		ll_student = (LinearLayout) findViewById(R.id.ll_student);
		ll_jiazhang = (LinearLayout) findViewById(R.id.ll_jiazhang);
		ll_youke = (LinearLayout) findViewById(R.id.ll_youke);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ll_teacher: //
			role = "2";
			popwindow("老师角色");
			break;
		case R.id.ll_xiaozhang: //
			role = "3";
			popwindow("校长角色");
			break;
		case R.id.ll_student: //
			role = "0";
			popwindow("学生角色");
			break;
		case R.id.ll_jiazhang: //
			role = "1";
			popwindow("家长角色");
			break;
		case R.id.ll_youke: //
			role = "4";
			popwindow("未报名体验区");
			break;
		}
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	private void popwindow(String str) {
		alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.show();
		window = alertDialog.getWindow();
		lp = window.getAttributes();
		lp.x = 0;
		window.setContentView(R.layout.pop_start);
		alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable());
		window.setLayout(dialogWidth, dialogHeight);

		TextView tv_content = (TextView) alertDialog.getWindow().findViewById(
				R.id.tv_content);
		tv_content.setText("跳转到" + str);

		new Handler().postDelayed(new Runnable() {

			@Override
			public void run() {
				
				//如果是游客身份直接跳转到应用选择界面(游客身份是不需要登录的)
				if("4".equals(role)){
					intent = new Intent(StartActivity.this, ButtonActivity.class);
					SPUtil.putString(getApplicationContext(), "role", role);
					SPUtils.put(getApplicationContext(), ConfigConstant.IS_LOGIN_ON, false);
					startActivity(intent);
					finish();
				}else{
					//不是游客则跳转到登录界面
					intent = new Intent(StartActivity.this, LoginActivity.class);
					intent.putExtra("role", role);
					startActivity(intent);
					finish();
				}
				
			}
		}, 1200);
	}
}
